package com.github.manx98.okdownloader.interceptor;

import okhttp3.Interceptor;
import okhttp3.OkHttp;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;


/**
 * 清除默认请求头
 * @author : manx98
 * @date : 2021/5/26
 */
public class ClearDefaultUserAgentInterceptor implements Interceptor {
    private static final String USER_AGENT = "User-Agent";
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();
        if(OkHttp.VERSION.equals(request.header(USER_AGENT))){
            request = request.newBuilder().removeHeader(USER_AGENT).build();
        }
        return chain.proceed(request);
    }

    private ClearDefaultUserAgentInterceptor() {}

    private static class Instance {
        static final ClearDefaultUserAgentInterceptor CLEAR_DEFAULT_USER_AGENT_INTERCEPTOR = new ClearDefaultUserAgentInterceptor();
    }

    public static ClearDefaultUserAgentInterceptor instance(){
        return Instance.CLEAR_DEFAULT_USER_AGENT_INTERCEPTOR;
    }
}
